﻿using System;
using System.Web.Compilation;
using System.Web;
using System.Globalization;
using System.Collections.Specialized;
using System.Collections;
using System.Resources;
using System.Data;
using NET.Library.Data;

namespace NET.Library.Resources.ResourceProviders
{
    public abstract class DBResourceProviderFactory : ResourceProviderFactory
    {
        public DBResourceProviderFactory()
        {
        }

        public override IResourceProvider
            CreateGlobalResourceProvider(string classKey)
        {
            return new DBResourceProvider(null, classKey);
        }

        public override IResourceProvider
           CreateLocalResourceProvider(string virtualPath)
        {
            virtualPath = System.IO.Path.GetFileName(virtualPath);
            return new DBResourceProvider(virtualPath, null);
        }

        private sealed class DBResourceProvider : IResourceProvider
        {
            //private string _virtualPath;
            private string _ClassName;
            private IDictionary _resourceCache;
            //private static object CultureNeutralKey = new object();
            private int _LanguageId;

            //public DBResourceProvider(string virtualPath, string className)
            public DBResourceProvider(string className)
            {
                //_virtualPath = virtualPath;
                _ClassName = className;
            }

            //private IDictionary GetResourceCache(string cultureName)
            private IDictionary GetResourceCache(int languageId)
            {
                //object cultureKey;
                //if (cultureName != null)
                //{
                //    cultureKey = cultureName;
                //}
                //else
                //{
                //    cultureKey = CultureNeutralKey;
                //}

                object languageKey = languageId;


                if (_resourceCache == null)
                {
                    _resourceCache = new ListDictionary();
                }
                //IDictionary resourceDict = _resourceCache[cultureKey] as IDictionary;
                IDictionary resourceDict = _resourceCache[languageKey] as IDictionary;
                if (resourceDict == null)
                {
                    //resourceDict = DBResourceHelper.GetResources(_virtualPath,
                    //              _ClassName, cultureName, false, null);
                    //resourceDict = DBResourceHelper.GetResources(_virtualPath,
                    //              _ClassName, cultureName, false, null);
                    resourceDict = DBResourceHelper.GetResources(_ClassName, languageId, false, null);
                    //_resourceCache[cultureKey] = resourceDict;
                    _resourceCache[languageKey] = resourceDict;
                }
                return resourceDict;
            }

            //object IResourceProvider.GetObject(string resourceKey, CultureInfo culture)
            object IResourceProvider.GetObject(string resourceKey, int languageId)
            {
                //string cultureName = null;
                //if (culture != null)
                //{
                //    cultureName = culture.Name;
                //}
                //else
                //{
                //    cultureName = CultureInfo.CurrentUICulture.Name;
                //}

                //object value = GetResourceCache(cultureName)[resourceKey];
                object value = GetResourceCache(languageId)[resourceKey];

                if (value == null)
                {
                    // resource is missing for current culture, use default

                    DBResourceHelper.AddResource(resourceKey,
                            _virtualPath, _className, cultureName);

                    value = GetResourceCache(null)[resourceKey];
                }
                if (value == null)
                {
                    // the resource is really missing, no default exists

                    DBResourceHelper.AddResource(resourceKey,
                         _virtualPath, _className, string.Empty);
                }
                return value;
            }

            IResourceReader IResourceProvider.ResourceReader
            {
                get
                {
                    return new DBResourceReader(GetResourceCache(null));
                }
            }
        }

        private sealed class DBResourceReader : IResourceReader
        {
            private IDictionary _resources;
            public DBResourceReader(IDictionary resources)
            {
                _resources = resources;
            }
            IDictionaryEnumerator IResourceReader.GetEnumerator()
            {
                return _resources.GetEnumerator();
            }
            void IResourceReader.Close()
            {
            }
            IEnumerator IEnumerable.GetEnumerator()
            {
                return _resources.GetEnumerator();
            }
            void IDisposable.Dispose()
            {
            }
        }

        internal static class DBResourceHelper
        {
            public static IDictionary GetResources(string className, int languageId,
                   bool designMode, IServiceProvider serviceProvider)
            {
                //////SqlConnection con = new SqlConnection(
                //////  System.Configuration.ConfigurationManager.
                //////  ConnectionStrings["your_connection_string"].ToString());
                //////SqlCommand com = new SqlCommand(); 
                //
                //////////////IDbCommand com;

                // Build correct select statement to get resource values

                //

                //if (!String.IsNullOrEmpty(virtualPath))
                //{
                //    //

                //    // Get Local resources

                //    //

                //    if (string.IsNullOrEmpty(languageId))
                //    { 
                //        // default resource values (no culture defined)

                //        com.CommandType = CommandType.Text;
                //        com.CommandText = "select resource_name, resource_value" + 
                //                          " from ASPNET_GLOBALIZATION_RESOURCES" + 
                //                          " where resource_object = @virtual_path" + 
                //                          " and culture_name is null";
                //        com.Parameters.AddWithValue("@virtual_path",virtualPath);
                //    }
                //    else
                //    {
                //        com.CommandType = CommandType.Text;
                //        com.CommandText = "select resource_name, resource_value" + 
                //                          " from ASPNET_GLOBALIZATION_RESOURCES " + 
                //                          "where resource_object = @virtual_path " + 
                //                          "and culture_name = @culture_name ";
                //        com.Parameters.AddWithValue("@virtual_path", virtualPath);
                //        com.Parameters.AddWithValue("@culture_name", languageId);
                //    }
                //}
                //else 
                ////////if (!String.IsNullOrEmpty(className))
                ////////{ 
                ////////    //

                ////////    // Get Global resources

                ////////    // 

                ////////    //if (string.IsNullOrEmpty(languageId))
                ////////    //{
                ////////    //    // default resource values (no culture defined)

                ////////    //    com.CommandType = CommandType.Text;
                ////////    //    com.CommandText = "select resource_name, resource_value" + 
                ////////    //                      " from ASPNET_GLOBALIZATION_RESOURCES " + 
                ////////    //                      "where resource_object = @class_name" + 
                ////////    //                      " and culture_name is null";
                ////////    //    com.Parameters.AddWithValue("@class_name", className);
                ////////    //}
                ////////    //else
                ////////    //{
                ////////        //////////////////////com.CommandType = CommandType.Text;
                ////////        //////////////////////com.CommandText = "select resource_name, resource_value " + 
                ////////        //////////////////////                  "from ASPNET_GLOBALIZATION_RESOURCES where " + 
                ////////        //////////////////////                  "resource_object = @class_name and" + 
                ////////        //////////////////////                  " culture_name = @culture_name ";
                ////////        //////////////////////com.Parameters.AddWithValue("@class_name", className);
                ////////        //////////////////////com.Parameters.AddWithValue("@culture_name", languageId);
                ////////   // }
                ////////}
                ////////else 
                ////////{
                ////////    //

                ////////    // Neither virtualPath or className provided,

                ////////    // unknown if Local or Global resource

                ////////    //

                ////////    throw new Exception("DBResourceHelper.GetResources()" + 
                ////////          " - className missing from parameters."); 
                ////////} 

                if (String.IsNullOrEmpty(className))
                {

                    //

                    // className provided,

                    // unknown if Local or Global resource

                    //

                    throw new Exception("DBResourceHelper.GetResources()" +
                          " - className missing from parameters.");
                }

                ListDictionary resources = new ListDictionary();
                try
                {
                    //////com.Connection = con;
                    //////con.Open();
                    //////SqlDataReader sdr = com.ExecuteReader(CommandBehavior.CloseConnection);

                    //////while (sdr.Read())
                    //////{
                    //////    string rn = sdr.GetString(sdr.GetOrdinal("resource_name"));
                    //////    string rv = sdr.GetString(sdr.GetOrdinal("resource_value"));
                    //////    resources.Add(rn, rv);
                    //////}

                    using (IDataReader dr = DB.ExecuteReader("usp_SiteText_GetByClassAndLanguage", className, languageId))
                    {
                        while (dr.Read())
                        {
                            string rn = DBHelper.GetDBString(dr, "SiteTextGroupName");
                            string rv = DBHelper.GetDBString(dr, "Text");
                            resources.Add(rn, rv);
                        }
                        if (!dr.IsClosed) { dr.Close(); }
                    }


                }
                catch (Exception e)
                {
                    throw new Exception(e.Message, e);
                }
                finally
                {
                    //////if (con.State == ConnectionState.Open)
                    //////{
                    //////    con.Close();
                    //////}
                }

                return resources;
            }

            public static void UpdateResource(int siteTextGroupId, string siteTextGroupName, string className, int languageId)
            {
                try
                {
                    DB.ExecuteNonQuery("usp_SiteText_Update", siteTextGroupId, siteTextGroupName, className, languageId);
                }
                catch (Exception e)
                {
                    throw new Exception(e.ToString());
                }
            }

            //public static void AddResource(string resource_name,
            //       string virtualPath, string className, string cultureName)
            public static void AddResource(string siteTextGroupName, string className, int languageId)
            {
                //SqlConnection con =
                //  new SqlConnection(System.Configuration.ConfigurationManager.
                //  ConnectionStrings["your_connection_string"].ToString());
                //SqlCommand com = new SqlCommand();
                //StringBuilder sb = new StringBuilder();
                //sb.Append("insert into ASPNET_GLOBALIZATION_RESOURCES " +
                //          "(resource_name ,resource_value," +
                //          "resource_object,culture_name ) ");
                //sb.Append(" values (@resource_name ,@resource_value," +
                //          "@resource_object,@culture_name) ");
                //com.CommandText = sb.ToString();
                //com.Parameters.AddWithValue("@resource_name", resource_name);
                //com.Parameters.AddWithValue("@resource_value", resource_name +
                //                            " * DEFAULT * " +
                //                            (String.IsNullOrEmpty(cultureName) ?
                //                            string.Empty : cultureName));
                //com.Parameters.AddWithValue("@culture_name",
                //    (String.IsNullOrEmpty(cultureName) ? SqlString.Null : cultureName));

                string resource_object = "UNKNOWN **ERROR**";

                //if (!String.IsNullOrEmpty(virtualPath))
                //{
                //    resource_object = virtualPath;
                //}
                //else if (!String.IsNullOrEmpty(className))
                //{
                //    resource_object = className;
                //}
                if (!String.IsNullOrEmpty(className))
                {
                    resource_object = className;
                }

                //com.Parameters.AddWithValue("@resource_object", resource_object);

                try
                {
                    //com.Connection = con;
                    //con.Open();
                    //com.ExecuteNonQuery();
                    DB.ExecuteNonQuery("usp_SiteText_Create", className, siteTextGroupName, languageId);
                }
                catch (Exception e)
                {
                    throw new Exception(e.ToString());
                }
                finally
                {
                    //if (con.State == ConnectionState.Open)
                    //    con.Close();
                }
            }
        }

    }

    

}

//    internal static class DBResourceHelper
//{
//    public static IDictionary GetResources(string virtualPath, 
//           string className, string cultureName, 
//           bool designMode, IServiceProvider serviceProvider)
//    {
//        //////SqlConnection con = new SqlConnection(
//        //////  System.Configuration.ConfigurationManager.
//        //////  ConnectionStrings["your_connection_string"].ToString());
//        //////SqlCommand com = new SqlCommand(); 
//        //

//        // Build correct select statement to get resource values

//        //

//        if (!String.IsNullOrEmpty(virtualPath))
//        {
//            //

//            // Get Local resources

//            //

//            if (string.IsNullOrEmpty(cultureName))
//            { 
//                // default resource values (no culture defined)

//                com.CommandType = CommandType.Text;
//                com.CommandText = "select resource_name, resource_value" + 
//                                  " from ASPNET_GLOBALIZATION_RESOURCES" + 
//                                  " where resource_object = @virtual_path" + 
//                                  " and culture_name is null";
//                com.Parameters.AddWithValue("@virtual_path",virtualPath);
//            }
//            else
//            {
//                com.CommandType = CommandType.Text;
//                com.CommandText = "select resource_name, resource_value" + 
//                                  " from ASPNET_GLOBALIZATION_RESOURCES " + 
//                                  "where resource_object = @virtual_path " + 
//                                  "and culture_name = @culture_name ";
//                com.Parameters.AddWithValue("@virtual_path", virtualPath);
//                com.Parameters.AddWithValue("@culture_name", cultureName);
//            }
//        }
//        else if (!String.IsNullOrEmpty(className))
//        { 
//            //

//            // Get Global resources

//            // 

//            if (string.IsNullOrEmpty(cultureName))
//            {
//                // default resource values (no culture defined)

//                com.CommandType = CommandType.Text;
//                com.CommandText = "select resource_name, resource_value" + 
//                                  " from ASPNET_GLOBALIZATION_RESOURCES " + 
//                                  "where resource_object = @class_name" + 
//                                  " and culture_name is null";
//                com.Parameters.AddWithValue("@class_name", className);
//            }
//            else
//            {
//                com.CommandType = CommandType.Text;
//                com.CommandText = "select resource_name, resource_value " + 
//                                  "from ASPNET_GLOBALIZATION_RESOURCES where " + 
//                                  "resource_object = @class_name and" + 
//                                  " culture_name = @culture_name ";
//                com.Parameters.AddWithValue("@class_name", className);
//                com.Parameters.AddWithValue("@culture_name", cultureName);
//            }
//        }
//        else 
//        {
//            //

//            // Neither virtualPath or className provided,

//            // unknown if Local or Global resource

//            //

//            throw new Exception("SqlResourceHelper.GetResources()" + 
//                  " - virtualPath or className missing from parameters."); 
//        } 
//        ListDictionary resources = new ListDictionary();
//        try
//        {
//            com.Connection = con;
//            con.Open();
//            SqlDataReader sdr = com.ExecuteReader(CommandBehavior.CloseConnection);
  
//            while (sdr.Read())
//            {
//                string rn = sdr.GetString(sdr.GetOrdinal("resource_name"));
//                string rv = sdr.GetString(sdr.GetOrdinal("resource_value"));
//                resources.Add(rn, rv);
//            }
//        }
//        catch (Exception e)
//        {
//            throw new Exception(e.Message, e); 
//        }
//        finally
//        {
//            if (con.State == ConnectionState.Open)
//            {
//                con.Close();
//            }
//        }
        
//        return resources;
//    }

//    public static void AddResource(string resource_name, 
//           string virtualPath, string className, string cultureName)
//    { 
//        SqlConnection con = 
//          new SqlConnection(System.Configuration.ConfigurationManager.
//          ConnectionStrings["your_connection_string"].ToString());
//        SqlCommand com = new SqlCommand(); 
//        StringBuilder sb = new StringBuilder();
//        sb.Append("insert into ASPNET_GLOBALIZATION_RESOURCES " + 
//                  "(resource_name ,resource_value," + 
//                  "resource_object,culture_name ) ");
//        sb.Append(" values (@resource_name ,@resource_value," + 
//                  "@resource_object,@culture_name) ");
//        com.CommandText = sb.ToString();
//        com.Parameters.AddWithValue("@resource_name",resource_name);
//        com.Parameters.AddWithValue("@resource_value", resource_name + 
//                                    " * DEFAULT * " + 
//                                    (String.IsNullOrEmpty( cultureName) ? 
//                                    string.Empty : cultureName ));
//        com.Parameters.AddWithValue("@culture_name", 
//            (String.IsNullOrEmpty(cultureName) ? SqlString.Null : cultureName));   
   
//        string resource_object = "UNKNOWN **ERROR**";
//        if (!String.IsNullOrEmpty(virtualPath))
//        {
//            resource_object = virtualPath;
//        }
//        else if (!String.IsNullOrEmpty(className))
//        {
//            resource_object = className;
//        }
//        com.Parameters.AddWithValue("@resource_object", resource_object);
   
//        try 
//        {
//            com.Connection = con;
//            con.Open();
//            com.ExecuteNonQuery();
//        }
//        catch (Exception e)
//        {
//            throw new Exception(e.ToString());
//        }
//        finally 
//        {
//            if (con.State == ConnectionState.Open)
//                con.Close();
//        } 
//    }
//}
